﻿# -*- coding: utf-8 -*-
'''eof
name:未销户（准）贷记卡授信额度使用占比
code:CardLimitUsedSituation_2D
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:
eof'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False



def IC5_CardLimitUsedSituation():
    data = IC_DSJ_2D['data']
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        table1=getTable(data,"PCO",1)#单张表
        if table1 is None:
            return u"缺失值"
        elif table1==[{}]:
            return u"缺失值"
        else:
            table3=getTable(table1[0],"PC02",0)
            if table3 is None:
                return u"缺失值"
            elif table3==[{}]:
                return u"缺失值"
            else:
                #贷记卡账户汇总信息段-单张
                table4a=getTable(table3[0],"PC02H",0)
                if table4a in [ None,[{}]]:
                    #贷记卡账户汇总信息段-已用额度
                    DJK_YYED =0
                    #贷记卡账户汇总信息段-授信总额
                    DJK_SXZE =0
                else:
                    if isfloat(table4a[0]['PC02HJ04'])==True and table4a[0].get('PC02HJ04') not in null_type_list:
                        DJK_YYED = float(table4a[0]['PC02HJ04'])
                    else:
                        #贷记卡账户汇总信息段-已用额度
                        DJK_YYED =0
                    if  isfloat(table4a[0]['PC02HJ01'])==True and table4a[0].get('PC02HJ01') not in null_type_list:
                        DJK_SXZE = float(table4a[0]['PC02HJ01'])
                    else:
                        #贷记卡账户汇总信息段-授信总额
                        DJK_SXZE =0
                #准贷记卡账户汇总信息段-单张
                table4b=getTable(table3[0],"PC02I",0)
                if table4b in [ None,[{}]]:
                    ZDJK_TZYE =0
                    ZDJK_SXZE =0
                else:
                    if  isfloat(table4b[0]['PC02IJ04'])==True and table4b[0].get('PC02IJ04') not in null_type_list:
                    #准贷记卡账户汇总信息段-透支余额
                        ZDJK_TZYE = float(table4b[0]['PC02IJ04'])
                    else:
                        ZDJK_TZYE = 0
                    if  isfloat(table4b[0]['PC02IJ01'])==True and table4b[0].get('PC02IJ01') not in null_type_list:
                        #准贷记卡账户汇总信息段-授信总额
                        ZDJK_SXZE = float(table4b[0]['PC02IJ01'])
                    else:
                        ZDJK_SXZE = 0
                if ZDJK_SXZE+DJK_SXZE ==0:
                    return u"缺失值"
                else:
                    return round((DJK_YYED+ZDJK_TZYE)/(ZDJK_SXZE+ DJK_SXZE),4)
    except:
        return u"缺失值"

result = IC5_CardLimitUsedSituation()



